(function($){
	
	$(document)
	.ready(function(){
		$('.deleteImageLink').click(deleteImageLinkFunc);
		$('.editTextLink').click(editTextLinkFunc);
		$('.editImageLink').click(function(){
			
			return false;
		});

	
	});
	
editTextLinkFunc = function(){
	var id = $(this).parents('table').attr('imageid');
	tmplog( id );
	tmplog( $('#'+id+'post_name') );
	buildDialogBox({
		post_title : $('#'+id+'post_title').val(),
		tags : $('#'+id+'tags').val(),
		post_title : $('#'+id+'post_title').val(),
		desc : $('#'+id+'post_content').val(),
		isactive : $('#'+id+'isactive').val()
	});
	return false;
};
	
deleteImageLinkFunc = function(){
	var url = $(this).attr('href')+'&'+new Date().getTime();
	var imageid = $(this).attr('imageid');
	var complete = false;
	if(confirm("Are you sure you want to delete this image?")){
		if( confirm("Do you want to delete this from the Media Library?\n\nHit ok for yes, Hit cancel for no") ){
			complete = true;
		}
		
		$.get(url,{_once : _once,complete:complete},function(data, textStatus){
			tmplog( textStatus );
			if( textStatus == "error" ){
			}
			else{
				$('table[imageid='+imageid+']').parent().parent().remove();
			}
		},'json');
	};
	return false;
};

buildDialogBox = function( data ){
	if( $('#pxdialog').size() ){ $('#pxdialog').remove(); }
	
	var noCheck = '<input name="albumPhotos_isactive" type="radio" value="0" ';
	noCheck +=  (data.isactive) ? '' : ' checked="checked" ';
	noCheck += '/>';
	var yesCheck = '<input name="albumPhotos_isactive" type="radio" value="1" ';
	yesCheck +=  (data.isactive) ? ' checked="checked" ' : '';
	yesCheck += '/>';
	
	var table = $('<table width="450" border="0" cellspacing="3" cellpadding="3">')
					.append(
						$('<tr>')
							.append('<td align="left" valign="top" class="label"><?php _e('Image Alt / Title', 'photoxhibit'); ?> </td>')
							.append('<td align="left" valign="top"><input type="text" id="albumPhotos_alt" name="albumPhotos_alt" value="'+data.post_title+'" class="input"/></td>')
					)
					.append(
						$('<tr>')
							.append('<td align="left" valign="top" class="label"><?php _e('Image Tags', 'photoxhibit'); ?> </td>')
							.append('<td align="left" valign="top"><input type="text" id="albumPhotos_tags" name="albumPhotos_tags" value="'+data.tags+'" class="input"/> <?php _e('seperate with commas', 'photoxhibit'); ?> </td>')
					)
					.append(
						$('<tr>')
							.append('<td align="left" valign="top" class="label"><?php _e('Is active?', 'photoxhibit'); ?> </td>')
							.append(
								$('<td>')
									.append('<label>'+yesCheck+'<?php _e('Yes', 'photoxhibit'); ?></label>')
									.append('<label>'+noCheck+'<?php _e('No', 'photoxhibit'); ?></label>')
							)
					)
					.append(
						$('<tr>')
							.append('<td align="left" valign="top"><?php _e('Description', 'photoxhibit'); ?>:</td>')
							.append('<td align="left" valign="top">&nbsp;</td>')
					)
					.append(
						$('<tr>')
							.append('<td colspan="2" align="left" valign="top"><textarea style="width:445px;height:100px" id="albumPhotos_desc" name="albumPhotos_desc">'+data.desc+'</textarea></td>')
					)

	var div = $('<div id="pxdialog">')
					.append('<div id="edit_image_msg" style="display:none"><?php _e('Changes have been made, click the arrow in the top right corner of the modal box to close.', 'photoxhibit'); ?></div>')
					.append('<input type="hidden" id="px_imageEdit_id" value="'+data.ID+'"/>')
					.append(table)
					.append('<p class="submit" style="margin: 0pt auto; width: 400px;"><input id="px_editImageSubmit" type="submit" value="<?php _e('Submit Changes', 'photoxhibit'); ?>" name="px_editImageSubmit"/></p>')
					.appendTo('body');
	div.dialog({
		draggable : false,
		width: 500,
		height: 345,
		modal: true,
		resizable : false,
		title : '<?php _e('Edit Attributes', 'photoxhibit'); ?>',
			overlay: { 
				opacity: 0.5, 
				background: "black" 
			} 
	});
};


	$(document)
	.ready(function(){

		$('#otherAttachments input[type=checkbox]')
		.change(function(){
			var el = $(this);
			var p = el.parent().parent();
			
			if( el.is(':checked') ){
				p.addClass('active');
			}
			else{
				p.removeClass('active');
			}
		});
		
		$('#btnAddImages')
		.click(function(){
			var cbs = $('#otherAttachments input[type=checkbox]:checked');
			
			cbs.each(function(){
				var el = $(this);
				var p = el.parent().parent();
				var o = {};
				o['albumPhotos_id'] = 0;
				o['albumPhotos_file'] = p.find('input[name=post_path]').val();
				o['post_title'] = p.find('input[name=post_title]').val();
				o['post_name'] = p.find('input[name=post_name]').val();
				o['tags'] = p.find('input[name=tags]').val();
				o['post_content'] = p.find('textarea').val();
				o['ID'] = p.find('input[name=ID]').val();
				o['div'] = p;
				
				$.getJSON(baseUrl+'set',{
						pxsecondary : 'albumImage',
						id  : o['ID'],
						aid : $('#aid').val(),
						_once : _once
					},
					function(data){
						o['albumPhotos_id'] = data.id;
						get_image_from_upload( o, o.name );
						o.div.remove();
					}
				);
			});
			
			return false;
		});
		
	});
})(pxjQuery);